﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pavilion.Locks
{
    public class LockPredicateMonitor : LockMonitorBase
    {
        public override ILock Lock { get { return lockItem; } }

        private Func<bool> predicate;
        private IPropertyChanged item;
        private ILock lockItem;

        public LockPredicateMonitor(IPropertyChanged item, Func<bool> predicate, ILock lockItem)
        {
            this.item = item;
            this.predicate = predicate;
            this.lockItem = lockItem;
        }

        public override void Dispose()
        {
            item.PropertyChanged -= GameTool_PropertyChanged;
            predicate = null;
            item = null;
        }

        public override void Load(GameItem gameItem)
        {
            item.PropertyChanged += GameTool_PropertyChanged;

            UpdateLock();
        }

        public override void UpdateLock()
        {
            if (predicate == null)
                return;
            
            IsOpen = predicate.Invoke();
        }

        private void GameTool_PropertyChanged(object sender, string propertyName) { UpdateLock(); }
    }
}
